import { message } from 'antd';
import { upPhonemail, upPassword, upAvatar } from './service';

const Model = {
  namespace: 'smManInforRule',
  state: {
    manageListData: {},
    tableRef: {},
  },
  effects: {
    *upPhonemail({ payload, resolve }, { call, put }) {
      const params = {
        ...payload,
        // pageNum: payload.current,
        // pageSize: payload.pageSize,
      };

      const response = yield call(upPhonemail, params);
      if (!response.error) {
        const { records, pages, totalNum } = response;
        const result = {
          data: records,
          page: pages,
          pageSize: payload.pageSize,
          success: true,
          total: totalNum,
        };
        resolve && resolve(result);
        message.success('手机号,邮箱修改成功！');
        yield put({
          type: 'save',
          payload: {
            manageListData: result,
          },
        });
      }
    },

    *upPassword({ payload, resolve }, { call }) {
      const response = yield call(upPassword, payload);
      if (!response.error) {
        resolve && resolve(response);
        message.success('密码修改成功！');
      }
    },
    *upAvatar({ payload, resolve }, { call, put }) {
      const response = yield call(upAvatar, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('头像更换成功！');

        yield put({
          type: 'tableReload',
        });
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
    tableReload(state) {
      const tableRef = state.tableRef || {};
      setTimeout(() => {
        tableRef.current && tableRef.current.reloadAndRest();
      }, 0);
      return { ...state };
    },
  },
};
export default Model;
